﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Utils.Controls;
using UniMana.Data;
using UniMana.Utils.Data;

namespace UniMana.Win.Training
{
    public partial class FrmSubjectClassCanInputScore : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_dbTraining = new DBMain();
        private bds m_bds = new bds();
        private bool m_bInput;
        private List<int> m_lIClassKey;
        private bool m_bAgree;
        private bool m_bInTask;

        private FrmScore_TaskInput m_frmTaskInPut;
        public FrmSubjectClassCanInputScore()
        {
            if (!UniMana.Data.SysUtils.CheckAccessByRule(Utils.Configs.RuleConfig.SCORETASKINPUT))
            {
                XtraMessageBox.Show("Bạn không có quyền này");
                return;
            }
            
            InitializeComponent();
            this.m_lIClassKey = new List<int>();
            //rdgTask_SelectedIndexChanged(null, null);
            

            FrmScore_StatusInput frmStatusInput = new FrmScore_StatusInput();
            frmStatusInput.TopLevel = false;
            frmStatusInput.Dock = DockStyle.Fill;
            frmStatusInput.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.xtraScrollableControl.Controls.Add(frmStatusInput);
            frmStatusInput.WindowState = FormWindowState.Maximized;
            frmStatusInput.Show();

            this.rdgTask.SelectedIndex = 0;
        }

        private void GetData()
        {
            DateTime now = UniMana.Data.SysDateTime.Now;
            this.m_bds.DataSource = this.m_dbTraining.m_DCReadOnly.Training_Subject_Classes.GetListCanInputOrCheckScore(this.m_dbTraining.m_DCReadOnly.Training_SubjectClass_ScoreTypes, this.m_dbTraining.m_DCReadOnly.Training_Score_TaskInputs,this.m_dbTraining.m_DCReadOnly.Personnel_Personnels, now, this.m_bInput,this.m_bInTask);
            this.gctTraining_Subject_Classes.DataSource = this.m_bds;
        }

        
        private void btnAgree_Click(object sender, EventArgs e)
        {
            vAgree();
        }

        private void vAgree()
        {
            if (this.grvTraining_Subject_Classes.SelectedRowsCount > 0)
            {
                this.m_lIClassKey.Clear();
                int[] row = this.grvTraining_Subject_Classes.GetSelectedRows();
                for (int i = 0; i < row.Length; i++)
                {
                    int classKey = this.grvTraining_Subject_Classes.GetRowCellValue(row[i], gclTraining_Subject_ClassKey).ToInt();
                    this.m_lIClassKey.Add(classKey);
                }
                this.m_bAgree = true;

                //this.m_frmTaskInPut.Load(this.m_lIClassKey, this.m_bInput);
                FrmScore_TaskInput frm = new FrmScore_TaskInput(this.m_lIClassKey, this.m_bInput);
                frm.ShowDialog();
                //this.Close();
            }
            else
            {
                XtraMessageBox.Show("Chưa chọn lớp.");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.m_bAgree = false;
            this.Hide();
            //this.Dispose();
        }

        public List<int> ListClassKey
        {
            get { return this.m_lIClassKey; }
        }

        public bool Agree
        {
            get { return this.m_bAgree; }
        }

       
        private void grvTraining_Subject_Classes_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                vAgree();
            }
            catch
            {

            }
        }

        private void gctTraining_Subject_Classes_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                vAgree();
            }
        }

        private void grvTraining_Subject_Classes_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            if (this.grvTraining_Subject_Classes.SelectedRowsCount > 1)
            {
                this.xtraScrollableControl1.Visible = true;
            }
            else
            {
                this.xtraScrollableControl1.Visible = false;
            }
        }


        private void rdgTask_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.rdgTask.SelectedIndex)
            {
                case 0:
                    //Phân công nhập điểm
                    this.m_bInput = true;
                    this.m_bInTask = false;
                    break;
                case 1:
                    //Phân công kiểm tra
                    this.m_bInput = false;
                    this.m_bInTask = false;
                    break;
                case 2:
                    //Đã phân công nhập nhưng chưa hoàn thành
                    this.m_bInput = true;
                    this.m_bInTask = true;
                    break;
                case 3:
                    //Đã phân công kiểm tra nhưng chưa hoàn thành
                    this.m_bInput = false;
                    this.m_bInTask = true;
                    break;
            }
            if (this.rdgTask.SelectedIndex <= 3)
            {
                if (!this.xtraScrollableControl.Controls[0].Visible)
                {
                    this.xtraScrollableControl.Controls[0].Visible = true;
                }
                if (this.xtraScrollableControl.Controls[1].Visible)
                {
                    this.xtraScrollableControl.Controls[1].Visible = false;
                }
                GetData();
            }
            else
            {
                if (!this.xtraScrollableControl.Controls[1].Visible)
                {
                    this.xtraScrollableControl.Controls[1].Visible = true;
                }
                if (this.xtraScrollableControl.Controls[0].Visible)
                {
                    this.xtraScrollableControl.Controls[0].Visible = false;
                }
            }
        }

        private void phânCôngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            vAgree();
        }


        private void xemBảngĐiểmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //|EDIT|
            /*
            if (this.grvTraining_Subject_Classes.SelectedRowsCount == 1)
            {
                int classKey = this.grvTraining_Subject_Classes.GetFocusedRowCellValue(gclTraining_Subject_ClassKey).ToInt();
                FrmScore_Input f = new FrmScore_Input(classKey, FrmScore_Input.eFuncTions.View);
                try
                {
                    foreach (XtraTabPage p in ((FrmTrainingManager)Application.OpenForms["FrmTrainingManager"]).xtcMain.TabPages)
                    {
                        if (p.Text == f.Text)
                        {
                            ((FrmTrainingManager)Application.OpenForms["FrmTrainingManager"]).xtcMain.TabPages.Remove(p);
                            break;
                        }
                    }
                }
                catch { }

                TabShowFrm.ShowTabbedMdi(f, ((FrmTrainingManager)Application.OpenForms["FrmTrainingManager"]).xtcMain);
            }*/
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.radioGroup1.SelectedIndex)
            {
                case 0:
                    //Đã phân công nhập nhưng chưa hoàn thành
                    this.m_bInput = true;
                    this.m_bInTask = true;
                    break;
                case 1:
                    //Đã phân công kiểm tra nhưng chưa hoàn thành
                    this.m_bInput = false;
                    this.m_bInTask = true;
                    break;
            }
            if (this.radioGroup1.SelectedIndex <= 1)
            {
                if (!this.xtraScrollableControl.Controls[0].Visible)
                {
                    this.xtraScrollableControl.Controls[0].Visible = true;
                }
                if (this.xtraScrollableControl.Controls[1].Visible)
                {
                    this.xtraScrollableControl.Controls[1].Visible = false;
                }
                GetData();
            }
            else
            {
                if (!this.xtraScrollableControl.Controls[1].Visible)
                {
                    this.xtraScrollableControl.Controls[1].Visible = true;
                }
                if (this.xtraScrollableControl.Controls[0].Visible)
                {
                    this.xtraScrollableControl.Controls[0].Visible = false;
                }
            }
        }

        private void gctTraining_Subject_Classes_Click(object sender, EventArgs e)
        {

        }

        
    }
}